package com.zwl.demo.component.service;

import com.zwl.demo.component.entity.Node;
import com.zwl.demo.component.repo.NodeEntityRepository;
import com.zwl.demo.component.repo.RelationEntityRepository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class NodeService {

    @Resource
    private NodeEntityRepository nodeEntityRepository;

    @Resource
    private RelationEntityRepository relationEntityRepository;

    public Node create(String title,Long pid){
        if(!StringUtils.hasText(title)){
            //todo exception
            return null;
        }
        //未传值代表新建顶级节点
        if(Objects.isNull(pid)){
            pid = 0L;
        }

        return null;


    }

    /**
     * 检查相同同级节点title是否重复
     * @param title 节点名称
     * @param pid 上级节点ID
     * @return true:重复  false:未重复
     */
    private Boolean checkNodeExist(String title,Long pid){
        List<Node> nodes = nodeEntityRepository.findAllByPid(pid);
        List<Node> collect = nodes.stream().filter(item -> item.getTitle().equals(title)).collect(Collectors.toList());
        return collect.size() > 0;
    }
}
